package com.fita.admin.web.controller.data;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.data.CitySearch;
import com.fita.admin.service.DataService;
import com.fita.admin.service.data.CountryService;
import com.fita.entity.data.City;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 城市管理
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/cities")
@MenuSetting("menu-data")
public class CityController {
    
    private Logger logger = LoggerFactory.getLogger(CityController.class);

    @Autowired
    @Qualifier("dataService")
    private DataService<City> cityService;
    
    @Autowired
    private CountryService countryService;

    /**
     * list
     *
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-city-management")
    public ModelAndView list(Page page) {
                      
        ModelAndView modelAndView = new ModelAndView("data/city/city-list");
        
        int totals = cityService.count(City.class);
        if (totals > 0) {

            List<City> cities = cityService.listByPage(City.class, page.with(totals));
            
            modelAndView.addObject("cities", cities);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        return "data/city/city-search";
    }
    
    /**
     * 
     * @param search
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-city-management")
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    public ModelAndView onSearch(CitySearch search, Page page, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("data/city/city-list");
        
        int totals = cityService.count(City.class, search.getPreparedFilter(false));
        if (totals > 0) {
            
            List<City> cities = cityService.search(City.class, search, page.with(totals));
            modelAndView.addObject("cities", cities);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter","page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("data/city/city-edit");
        
        City city = cityService.load(City.class, id);
        
        modelAndView.addObject("city", city);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param id
     * @param city
     * @return
     */
    @RequestMapping(value = "/{id}" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@PathVariable(name="id") Long id, @Preloading("cityId") City city) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            cityService.update(city);
            
            httpResult.success().message("编辑城市成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("编辑城市失败");
            logger.error("#onEdit() - edit fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
    /**
     * 
     * @return
     */
/*    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport() {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("city.xls", "city.xls"));
        
        List<City> cities = cityService.listAll(City.class);
        modelAndView.addObject("cities", cities);
        
        List<Country> countries = countryService.listAll(Country.class);
        modelAndView.addObject("countries", CollectionUtils.constructMapValue(countries, "code"));
        
        return modelAndView;
        
    }*/
    
}
